package ltd.dujiabao.routingdatasource.config;

import com.zaxxer.hikari.HikariDataSource;
import ltd.dujiabao.routingdatasource.datasource.SwitchableDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author Bowie Du
 * @date 2025/11/1 00:10
 */

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "ltd.dujiabao",
        transactionManagerRef = "transactionManager",
        entityManagerFactoryRef = "entityManagerFactory")
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.old")
    public DataSourceProperties oldDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.old.hikari")
    public HikariDataSource oldDataSource() {
        return oldDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.new")
    public DataSourceProperties newDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.new.hikari")
    public HikariDataSource newDataSource() {
        return newDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    @Primary
    public DataSource switchableDataSource(SwitchDataSourceProperties properties) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceMode.OLD, oldDataSource());
        targetDataSources.put(DataSourceMode.NEW, newDataSource());

        SwitchableDataSource dataSource = new SwitchableDataSource(properties);
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(oldDataSource());
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                                                                       DataSource switchableDataSource) {
        return builder
                .dataSource(switchableDataSource)
                .packages("ltd.dujiabao")
                .build();
    }

    @Bean
    public PlatformTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactoryOne) {
        return new JpaTransactionManager(Objects.requireNonNull(entityManagerFactoryOne.getObject()));
    }
}
